﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhiJiao.Dal;
using ZhiJiao.Models.Entities;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;

namespace ZhiJiao.Service.Services
{
    public class HomeService:IHomeService
    {
        private readonly ICurrentUser _currentUser;
        private readonly UserManager<User> _userManager;
        private readonly BackJson _res;
        private readonly ZJDbContext _db;
        public HomeService(ICurrentUser currentUser,
                           UserManager<User> userManager,
                           ZJDbContext dbContext,
                           BackJson backJson) 
        {
            _currentUser=currentUser;
            _userManager=userManager;
            _res=backJson;
            _db=dbContext;
        }

        /// <summary>
        /// 用户模块集合
        /// tree
        /// </summary>
        public async Task<BackJson> GetUserModules()
        {
            var user = _currentUser.User;
            // 获取角色
            IList<string>? roloList = await _userManager.GetRolesAsync(user);
            List<Role> roles = new List<Role>();
            foreach (var roleName in roloList)
            {
                var role = await _db.Roles.Include(r => r.Modules)
                                            .ThenInclude(m=>m.Children)
                                            .FirstOrDefaultAsync(r => r.Name.Contains(roleName));
                roles.Add(role);
            }
            if (roles.Count > 0)
            {
                List<Module> modules = new List<Module>();
                foreach (var role in roles)
                {
                    foreach (var module in role.Modules)
                    {
                        if (modules.FirstOrDefault(m => m.Id == module.Id) == null)
                        {
                            modules.Add(module);
                        }
                    }
                }
                if (modules.Count > 0)
                {
                    var result = modules.Where(m => m.ParentModule == null || m.IsDirectory).OrderBy(m=>m.SortNum).Select(m => new
                    {
                        id = m.Id,
                        name = m.Name,
                        url = m.Url,
                        icon = m.Icon,
                        isDirectory = m.IsDirectory,
                        children = m.Children?.Select(child => new
                        {
                            id = child.Id,
                            name = child.Name,
                            url = child.Url,
                            icon = child.Icon,
                            isDirectory = child.IsDirectory
                        })
                    });
                    _res.code = 0;
                    _res.data = result;
                    _res.msg = "获取用户模块成功";
                }
                else
                {
                    _res.code = 1;
                    _res.msg = "用户的角色中暂无任何权限";
                }
            }
            else {
                _res.code = 1;
                _res.msg = "还未给用户配置任何角色";
            } 
           

            // 根据
            return _res;
        }
    }
}
